package com.umeox.recorder

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import androidx.appcompat.app.AppCompatDialog
import com.umeox.moto.common.utils.FileUtils
import com.umeox.recorder.data.RecorderBean
import com.umeox.recorder.data.RecorderDataBase
import com.umeox.recorder.databinding.DialogRecordDeleteBinding
import java.io.File
import java.util.Locale


class RecordDeleteDialog(
    context: Context,
    private inline val onDelete: (() -> Unit)? = null
) : AppCompatDialog(context) {

    private lateinit var mBinding: DialogRecordDeleteBinding
    private var mRecord: RecorderBean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setCanceledOnTouchOutside(false)
        mBinding = DialogRecordDeleteBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        val window = window
        if (window != null) {
            // 设置窗口为全屏
            window.setLayout(MATCH_PARENT, MATCH_PARENT)
            // 可选：移除系统装饰，如状态栏和导航栏
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }

        mBinding.btnDelete.setOnClickListener {
            mRecord?.let {
                RecorderDataBase.getInstance(context).recorderDao.delete(it)
                FileUtils.deleteFile(it.filePath)
                mRecord = null
            }
            onDelete?.invoke()
            cancel()
        }
    }

    fun show(record: RecorderBean) {
        super.show()
        mRecord = record
        val name = mRecord?.filePath?.let {
            "\u200E${File(it).name}"
        } ?: return
        mBinding.tvWarn.text = String.format(Locale.getDefault(), context.getString(R.string.str_remove), name)
    }

    override fun onBackPressed() {
        dismiss()
    }
}
